<template>
  <p>
    <button @click="add">
      <i class="ace-icon fa fa-edit"></i>
      新增
    </button>
    &nbsp;
    <button @click="refresh">
      <i class="ace-icon fa fa-undo"></i>
      刷新
    </button>
  </p>
  <table id="simple-table" class="table  table-bordered table-hover">
    <thead>
      <tr><#list fieldList as field><#if field.nameHump!="createdAt" && field.nameHump!="updatedAt">
          <th>${field.nameCn}</th></#if></#list>
          <th>操作</th>
      </tr>
    </thead>

    <tbody>
    <tr v-for="${domain} in ${domain}s" :key="${domain}.id">
      <#list fieldList as field>
        <#if field.nameHump!="createdAt" && field.nameHump!="updatedAt">
          <#if field.enums><td>{{ optionKV(${field.enumsConst}_REF, ${domain}.${field.nameHump}) }}</td>
<#else><td>{{ ${domain}.${field.nameHump} }}</td>
          </#if>
        </#if>
      </#list>
      <td>
        <div class="hidden-sm hidden-xs btn-group">

          <button @click="edit(${domain})" class="btn btn-xs btn-info">
            <i class="ace-icon fa fa-pencil bigger-120"></i>
          </button>

          <button @click="del(${domain}.id)" class="btn btn-xs btn-danger">
            <i class="ace-icon fa fa-trash-o bigger-120"></i>
          </button>
        </div>

        <div class="hidden-md hidden-lg">
          <div class="inline pos-rel">
            <button class="btn btn-minier btn-primary dropdown-toggle" data-toggle="dropdown" data-position="auto">
              <i class="ace-icon fa fa-cog icon-only bigger-110"></i>
            </button>

            <ul class="dropdown-menu dropdown-only-icon dropdown-yellow dropdown-menu-right dropdown-caret dropdown-close">
              <li>
                <a href="#" class="tooltip-info" data-rel="tooltip" title="View">
																			<span class="blue">
																				<i class="ace-icon fa fa-search-plus bigger-120"></i>
																			</span>
                </a>
              </li>

              <li>
                <a href="#" class="tooltip-success" data-rel="tooltip" title="Edit">
																			<span class="green">
																				<i class="ace-icon fa fa-pencil-square-o bigger-120"></i>
																			</span>
                </a>
              </li>

              <li>
                <a href="#" class="tooltip-error" data-rel="tooltip" title="Delete">
																			<span class="red">
																				<i class="ace-icon fa fa-trash-o bigger-120"></i>
																			</span>
                </a>
              </li>
            </ul>
          </div>
        </div>
      </td>
    </tr>
    </tbody>
  </table>
  <!-- 分页插件 -->
  <pagination ref="paginationRef" :list="get${Domain}List" />

  <!-- Modal -->
  <div class="modal fade" id="form-modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
          <h4 class="modal-title" id="myModalLabel">表单</h4>
        </div>
        <div class="modal-body">
          <form class="form-horizontal">
            <#list fieldList as field>
               <#if field.nameHump!="id" && field.nameHump!="createdAt" && field.nameHump!="updatedAt">
                 <div class="form-group">
                   <label class="col-sm-2 control-label">${field.nameCn}</label>
                   <div class="col-sm-10">
                 <#if field.enums>
                 <select v-model="${domain}.${field.nameHump}" class="form-control">
                   <option v-for="o in ${field.enumsConst}_REF" :value="o.key">{{ o.value }}</option>
                 </select>
                     <#else>
                   <input v-model="${domain}.${field.nameHump}" type="text" class="form-control">
                 </#if>
                </div>
              </div>
              </#if>
            </#list>
          </form>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
          <button @click="save${Domain}" type="button" class="btn btn-primary">保存</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import axios from "axios";
import {onMounted, ref} from "vue";
import pagination from "@/components/pagination";
<#list fieldList as field>
<#if field.enums>
import Filter from "@/filter/filter"
<#break>
</#if>
</#list>

export default {
  name: "${Domain}",
  components: {
    pagination,
  },
  setup() {
    // ${domain}s 数组
    const ${domain}s = ref([]);
    const ${domain} = ref({}); // 保存${tableNameCn}表单
    const paginationRef = ref(null);

    // 枚举字段
    <#list fieldList as field>
      <#if field.enums>
    const ${field.enumsConst}_REF = ref();
      </#if>
    </#list>

    // 刷新
    const refresh = () => {
      get${Domain}List(1);
    }

    // 获取${tableNameCn}列表
    const get${Domain}List = (page) => {
      Loading.show();
      axios.post(process.env.VUE_APP_SERVER + "/${module}/admin/${domain}/list",
          {
            page: page,
            size: paginationRef.value.size
          }
      ).then((response) => {
        Loading.hide();
        const resp = response.data;
        ${domain}s.value = resp.content.list;
        paginationRef.value.render(page, resp.content.total);
      })
    }

    // 保存${tableNameCn}
    const save${Domain} = () => {
      // 保存参数校验
      if ( 1 != 1
              <#list fieldList as field>
              <#if field.nameHump!="createdAt" && field.nameHump!="updatedAt" && field.nameHump!="sort">
               <#if !field.nullAble>
                || !Validator.require(${domain}.value.${field.nameHump}, "${field.nameCn}")
               </#if>
              <#if (field.length > 0)>
                || !Validator.length(${domain}.value.${field.nameHump}, "${field.nameCn}", 1, ${field.length?c})
              </#if>
              </#if>
              </#list>
        )
      {
        return;
      }

      Loading.show();
      axios.post(process.env.VUE_APP_SERVER + "/${module}/admin/${domain}/save", ${domain}.value).then((response) => {
        const resp = response.data;
        if (resp.success) {
          Loading.hide();
          $('#form-modal').modal('hide');
          Toast.success("保存成功");
          get${Domain}List(1);
        } else {
          Loading.hide();
          $('#form-modal').modal('hide');
          Toast.warning(resp.message);
        }
      })
    }

    // 删除${tableNameCn}
    const del = (id) => {
      Confirm.show("删除后不可恢复，确认删除？", function () {
        Loading.show();
        axios.delete(process.env.VUE_APP_SERVER + "/${module}/admin/${domain}/delete/" + id).then((response) => {
          const resp = response.data;
          if (resp.success) {
            //$('#form-modal').modal('hide');
            get${Domain}List(1);
          }
        })
        Toast.success("删除成功");
      })
    }

    // 添加${tableNameCn}
    const add = () => {
      ${domain}.value = {};
      $('#form-modal').modal('show');
    }

    // 修改${tableNameCn}
    const edit = (param) => {
      // 方法一
      //${domain}.value = JSON.parse(JSON.stringify(param));
      // 方法二
      ${domain}.value = $.extend({}, param);
      $('#form-modal').modal('show');
    }

    <#list fieldList as field>
    <#if field.enums>
    // 渲染收费字段 收费 状态 级别
    const optionKV = (object, key) => {
      return Filter.optionKV(object, key);
    }
    <#break>
    </#if>
    </#list>

    // 初始加载第一页
    onMounted(() => {
      get${Domain}List(1);
      <#list fieldList as field>
      <#if field.enums>
      ${field.enumsConst}_REF.value = ${field.enumsConst};
      </#if>
      </#list>
    })

    return {
      ${domain}s,
      refresh,
      paginationRef,
      get${Domain}List,
      add,
      save${Domain},
      ${domain},
      edit,
      del,
      <#list fieldList as field>
      <#if field.enums>
      ${field.enumsConst}_REF,
      </#if>
      </#list>
      <#list fieldList as field>
      <#if field.enums>
      optionKV,
      <#break>
      </#if>
      </#list>
    }
  }
}
</script>

<style scoped>

</style>